from django.shortcuts import render, HttpResponse
from .models import User, Article

# Create your views here.
def article_test(request):
    user = User(username="张飞", password="111111") # 定义父表相关字段
    user.save() # 保存到数据库中
    # 定义子表相关字段
    article = Article(title="我叫张飞~", content="我张飞三级不开大！！！", author=user)
    article.save()

    return HttpResponse("测试成功！")

def use_primary(request):
    article = Article.objects.first()
    return HttpResponse(article.author.username) # 通过子表的外键获取父表的相关信息

def onetomany(request): # 通过父表属性找到子表属性
    user = User.objects.first()
    # articles = user.article_set.all()
    articles = user.articles.all()
    for article in articles:
        print(article.title, article.content)

    return HttpResponse("onetomany success!")